package com.health.mapp.utils;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

public class StringUtils {

	
	public static boolean isBlank(String str)
	{
		
		if(null==str || "".equals(str) ||"null".equals(str))
		{
			return true;
		}
		return false;
	}
	
	 public static String beanToJson(Object bean) {   
         StringBuilder json = new StringBuilder();   
         json.append("[");   
         PropertyDescriptor[] props = null;   
        try {   
        	if (null != bean) {
      		  props = Introspector.getBeanInfo(bean.getClass(), Object.class)   
                .getPropertyDescriptors(); 
			}
        	else{
				return "[]";
			}
         } catch (IntrospectionException e) {   
         }   
        if (props != null) {   
            for (int i = 0; i < props.length; i++) {   
                try {  
                	
                	
                     String name = props[i].getName();   
                   
                     String value = String.valueOf(props[i].getReadMethod().invoke(bean));  
                    
                     if (!StringUtils.isBlank(value)) {
                    	 json.append(name);   
	                     json.append(":");   
	                     json.append(value);   
	                     json.append(",");  
					}
                 } catch (Exception e) { 
                	 e.printStackTrace();
                 }   
             }   
             json.setCharAt(json.length() - 1, ']');   
         } else {   
             json.append("]");   
         }   
        return json.toString();   
     }   
		/**
		 * @param srcString
		 * @return
		 */
		private static String toUpperCaseInitial(String srcString) {
			StringBuilder sb = new StringBuilder();
			sb.append(Character.toUpperCase(srcString.charAt(0)));
			sb.append(srcString.substring(1));
			return sb.toString();
		}
	 public static Object jsonToBean(String className,  String jsonStr)
	 {
		 
		 	if(null==jsonStr ||"".equals(jsonStr))
		 	{
			
		 		return  null;
		 	}
		 
		//对应的请求bean
			StringBuffer beanClassName = new StringBuffer();
			beanClassName.append("com.hris.db.model.");
			beanClassName.append(toUpperCaseInitial(className));
		 

		 
			
			//要映射为的对象
			Object jsonBean = null;
			try {
			 
			  
				//反射生成请求类对象
				//这里是一个空的请求模板
				Class<?> reqClass = Class.forName(beanClassName.toString());
				
				//对象映射器
				Object objectMapper = new Object();
				//objectMapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);  
				//objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
				
			

				 
					
					//将传入的key=value
				      StringBuffer sb = new StringBuffer();
				      //目前还没清楚为什么字节数组不能直接进行对象转换，所以先用字符串进行处理
				 //	 for(int i=0;i<jsonBuffer.length;i++)
				 	// {
				 		//logger.debug("jsonBuffer-"+i+":"+ (char)jsonBuffer[i]);
				 //		sb.append((char)jsonBuffer[i]);
				 //	 }
				  
				 
					// logger.debug("jsonBuffer:"+ jsonBuffer.toString());
				//	jsonBean = objectMapper.readValue(new String(jsonBuffer), reqClass);
				     // System.out.println(jsonStr);
					JSONObject obj = JSONObject.parseObject(jsonStr);
					jsonBean =     JSONObject.toJavaObject(obj, reqClass);
			        //System.out.println(objectMapper);

				 
			}
			catch(Exception e)
			{
				e.printStackTrace();
				
			}
			return jsonBean;
	 }
	 
	 public static String beanToJsonV2(Object bean) {  
		 JSONObject jsonObj1 = (JSONObject) JSONObject.toJSON(bean);  
		 String json=jsonObj1.toJSONString();
		 
		 return json;
	 }
	 
	/**
	 * 获取当前时间 
	 * @format yyyyMMddHHmmss
	 * @return
	 * @throws Exception
	 */
	public static String getSysTimeYYMMDDHHMMSS() {
		String re ="";
		String format = "yyyyMMddHHmmss";
		SimpleDateFormat sf = new SimpleDateFormat(format);
		Calendar calendar = new GregorianCalendar();
		re = sf.format(calendar.getTime()).toString();
		return re;
	}
	
	/**
	 * 计算时间间隔
	 * @param startdate  开始时间
	 * @param enddate    结束时间
	 * @return long      时间间隔
	 */
	public static long timeInter(String startdate, String enddate) {
		long m_intervalday = 0;//初始化时间间隔的值为0
		//使用的时间格式为yyyy-MM-dd
		SimpleDateFormat m_simpledateformat = new SimpleDateFormat("yyyyMMddHHmmss");
		try {
		//创建两个Date对象
		Date date1;
		Date date2 ;
		try {
			date1 = m_simpledateformat.parse(startdate);
			date2 = m_simpledateformat.parse(enddate);
			m_intervalday = date2.getTime() - date1.getTime();//计算所得为微秒数
			m_intervalday = m_intervalday / 1000 ;//计算所得的天数
		} catch (java.text.ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		} catch (ParseException e) {
		e.printStackTrace();
		}
		return m_intervalday;
		 
	}
	
	/**
	 * 计算时间间隔
	 * @param startdate  开始时间
	 * @param enddate    结束时间
	 * @return long      时间间隔
	 */
	public static long timeInter(String startdate, String enddate,String fomat) {
		long m_intervalday = 0;//初始化时间间隔的值为0
		//使用的时间格式为yyyy-MM-dd
		SimpleDateFormat m_simpledateformat = new SimpleDateFormat(fomat);
		try {
		//创建两个Date对象
		Date date1;
		Date date2 ;
		try {
			date1 = m_simpledateformat.parse(startdate);
			date2 = m_simpledateformat.parse(enddate);
			m_intervalday = date2.getTime() - date1.getTime();//计算所得为微秒数
			m_intervalday = m_intervalday / 1000 ;//计算所得的秒数
		} catch (java.text.ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		} catch (ParseException e) {
		e.printStackTrace();
		}
		return m_intervalday;
		 
	}
	
	 public static String beanToJsonT(Object bean) {   
         StringBuilder json = new StringBuilder();   
         json.append("[");   
         PropertyDescriptor[] props = null;   
        try {   
        	if (null != bean) {
      		  props = Introspector.getBeanInfo(bean.getClass(), Object.class)   
                .getPropertyDescriptors(); 
			}
        	else{
				return "[]";
			}
         } catch (IntrospectionException e) {   
         }   
        if (props != null) {   
            for (int i = 0; i < props.length; i++) {   
                try {  
                	
                	
                     String name = props[i].getName();   
                     System.out.println(props[i].getReadMethod().getClass().getCanonicalName());
                     String value = String.valueOf(props[i].getReadMethod().invoke(bean));  
                    
                     if (!StringUtils.isBlank(value)&&!("0").equals(value)) {
                    	 json.append(name);   
	                     json.append(":");   
	                     json.append(value);   
	                     json.append(",");  
					}
                 } catch (Exception e) { 
                	 e.printStackTrace();
                 }   
             }   
             json.setCharAt(json.length() - 1, ']');   
         } else {   
             json.append("]");   
         }   
        return json.toString();   
     } 
}
